<template>
  <div>
    <el-row class="disfena">
      <el-col>
        <p>管理员日志日志记录，点击 "查询" 安装选取条件查询日志</p>
      </el-col>
      <el-col :span="10" style="text-align: right;">
        <!-- <el-button type="primary" @click="importAdminControl = true">导入</el-button>
        <el-button type="primary" @click="daochus">导出</el-button> -->
        <el-button type="primary" @click="rizchaxu">查询</el-button>
      </el-col>
    </el-row>
    <com-table ref="comTable" :tableData="tableData" url="system/log/local" keyName="XPRO_ARRAY" :has-data="hasData"
      :table-head-list="tableHeadList" :total-count="totalCount" :page-num="pageNum" :searchForm="identform"
      :page-size="pageSize" :page-sizes="pageSizes" :table-loading="loading" :table-height="tableHeight"
      :is-show-select="true" :page-show="true" :handle-show="false" :merge-flag="false" @getTabpation="getTabpation"
      @setSearchHeight="setTableHeight">
    </com-table>
    <el-dialog title="日志查询" :visible="showAuditDialog" @close="showAuditDialog = false"
      custom-class="alarm-type-detail el-dialog-widthSmall" :close-on-click-modal="false" @closed="auditDialogClosed">
      <div class="dengshus">
        <el-form label-width="100px" :model="formInline">
          <!-- <el-row>
            <el-form-item label="操作员名称">
              <el-input v-model="formInline.KeySerial" clearable placeholder="请输入操作员名称"></el-input>
            </el-form-item>
          </el-row> -->
          <el-row>
            <el-form-item label="操作类型">
              <el-select v-model="formInline.Type" placeholder="请选择操作类型" clearable>
                <el-option v-for="item in liexions" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="起始日期">
              <el-date-picker v-model="formInline.beginTime" format="yyyy 年 MM 月 dd 日" value-format="timestamp"
                type="date" placeholder="选择起始日期">
              </el-date-picker>
            </el-form-item>
          </el-row>
          <el-row>
            <el-form-item label="终止日期">
              <el-date-picker v-model="formInline.endTime" format="yyyy 年 MM 月 dd 日" value-format="timestamp"
                type="date" placeholder="选择终止日期">
              </el-date-picker>
            </el-form-item>
          </el-row>
        </el-form>
      </div>
      <p>说明：默认为查询一个月内相关日志</p>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showAuditDialog = false">取 消</el-button>
        <el-button type="primary" @click="confirmInfo">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 导入 -->
    <data-import-dialog title="选择导入文件" :show.sync="importAdminControl" exportTemplateUrl="./static/export-template.xlsx"
      importDataUrl="/aqdl/auditlog/importlogrecord" extendFileName="xlsx,xls" @importCallback="theImportCallback"
      requestType="localhost" custom-class="equip-detail-deviceInfo" :pageType="pageType"></data-import-dialog>
  </div>
</template>
  
<script>
import ComTable from "../../components/table/comTable";
import dataImportDialog from "../../components/common/dataImportDialog.vue";
import { E_actType, E_keyType } from "../../assets/js/enum";
export default {
  name: "guanli",
  data() {
    return {
      empIds: "",
      empNames: "",
      E_actType:
        E_actType,
      E_keyType: E_keyType,
      accountNameOnline: '',
      pageNum: 1, // 当前页
      pageSize: 10,
      pageSizes: [5, 10, 20, 100],
      totalCount: 0, //共多少条数据
      tableHeight: "", // 表格高度
      hasData: true,
      showAuditDialog: false,
      formInline: {
        Type: "0",
        beginTime: "",
        endTime: "",
      },
      tableData: [],
      identform: {},
      loading: false,
      value1: "",
      tableHeadList: [
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "系统账户",
          columnName: "Account",
        },
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "操作员角色",
          columnName: "Role",
        },
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "操作类型",
          columnName: "Type",
        },
        // {
        //   colDisabled: false,
        //   colHidden: false,
        //   colSearch: true,
        //   colSort: false,
        //   columnComment: "操作动作",
        //   columnName: "AccountName",
        // },
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "时间",
          columnName: "TimeStamp",
        },
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "Key序列号",
          columnName: "KeySerial",
        },
        {
          colDisabled: false,
          colHidden: false,
          colSearch: true,
          colSort: false,
          columnComment: "Key类型",
          columnName: "KeyType",
        },
        // {
        //   colDisabled: false,
        //   colHidden: false,
        //   colSearch: true,
        //   colSort: false,
        //   columnComment: "备注",
        //   columnName: "DevIP",
        // },
      ],
      liexions: [
        { label: "全部", value: "0" },
        {
          label: "登录",
          value: "257",
        },
        {
          label: "登录失败",
          value: "258",
        },
        {
          label: "拔key注销",
          value: "259",
        },
        {
          label: "拔key锁屏",
          value: "260",
        },
        {
          label: "拔key重启",
          value: "261",
        },
        {
          label: "拔key关机",
          value: "262",
        },
        {
          label: "系统重启",
          value: "263",
        },
        {
          label: "系统关机",
          value: "264",
        },
        {
          label: "系统注销",
          value: "265",
        },
        {
          label: "系统锁屏",
          value: "266",
        },
        {
          label: "特权码登录",
          value: "267",
        },
        {
          label: "超时锁屏",
          value: "268",
        },
        {
          label: "登录用户名不匹配",
          value: "269",
        },
        {
          label: "修改pin码",
          value: "280",
        },
        {
          label: "解锁用户key",
          value: "290",
        },
        {
          label: "pin码锁定",
          value: "291",
        },
        {
          label: "绑定用户",
          value: "292",
        },
        {
          label: "解绑用户",
          value: "293",
        },
        {
          label: "注册用户",
          value: "294",
        },
        {
          label: "删除用户",
          value: "295",
        },
        {
          label: "校验pin码",
          value: "296",
        },
        {
          label: "查看key所属用户信息",
          value: "321",
        },
        {
          label: "查看key绑定信息",
          value: "322",
        },
        {
          label: "查看日志",
          value: "323",
        },
        {
          label: "导入日志",
          value: "324",
        },
        {
          label: "导出日志",
          value: "325",
        },
        {
          label: "设置配置参数",
          value: "326",
        },
        {
          label: "指纹录入",
          value: "353",
        },
        {
          label: "指纹删除",
          value: "354",
        },
        {
          label: "指纹解锁",
          value: "355",
        },
        {
          label: "指纹验证",
          value: "356",
        },
        {
          label: "卸载客户端",
          value: "369",
        },
      ],
      importAdminControl: false,
      pageType: "1",
      timestamp: "",
      regTime: '',
      obsefo: {}
    };
  },
  created() {
    this.setTableHeight();
    // this.setlogrlist("323", "0", "成功");
    this.getHostInfo()
  },
  props: {
    userName: {
      type: String,
      default: "",
    },
  },
  components: {
    ComTable,
    dataImportDialog,
  },
  methods: {
    getHostInfo() {
      this.$get(`/vgmp/config/gethostinfo`).then((res) => {
        console.log(res.data.data);
        this.empIds = res.data.data.empID
        this.empNames = res.data.data.empName
        this.regTime = res.data.data["regInfoTime"] * 1000
        this.formInline.beginTime = this.regTime
        this.formInline.endTime = new Date().getTime()
        console.log(this.regTime)
        this.getUserData()
      });
    },
    //获取账户
    getUserData() {
      this.$get("/aqdl/user/getloginuser").then((res) => {
        console.log(res, 293);
        this.accountNameOnline = res.data.data
        this.confirmInfo();
      });
    },
    // 记录操作日志接口
    setlogrlist(tyid, sults, redusl) {
      let eTimes = (new Date().getTime() + '').substring(0, 10)
      console.log(eTimes, "Timestamp")
      let that = this
      let KeySerial = '- -'
      this.$get("/aqdl/key/list_key").then(res => {
        console.log(res)
        if (res.data.data.keyArray.length != 0) {
          KeySerial = res.data.data.keyArray[0].keySn
        }
        let daJson = {
          "Root": {
            "LogType": "50",
            "LogRecord": {
              "Timestamp": eTimes + '',
              "EmpID": this.empIds,
              "EmpName": this.empNames,
              "Type": tyid,
              "KeySerial": KeySerial,
              "KeyType": "273",
              "Account": this.accountNameOnline,
              "Role": "admin",
              "Result": sults + '',
              "ResultDesc": redusl,
            },
          },
        };
        that.$postForJson("/aqdl/auditlog/setlogrecord", daJson).then((res) => {
          console.log(res);
        });
      })
    },
    //10位时间戳转换
    getLocalTime(timeStamp) {
      var date = new Date();
      date.setTime(timeStamp * 1000);
      var y = date.getFullYear();
      var m = date.getMonth() + 1;
      m = m < 10 ? ('0' + m) : m;
      var d = date.getDate();
      d = d < 10 ? ('0' + d) : d;
      var h = date.getHours();
      h = h < 10 ? ('0' + h) : h;
      var minute = date.getMinutes();
      minute = minute < 10 ? ('0' + minute) : minute;
      var second = date.getSeconds();
      second = second < 10 ? ('0' + second) : second;
      return y + '-' + m + '-' + d + ' ' + h + ':' + minute + ':' + second;
    },
    confirmInfo() {
      console.log(this.formInline.beginTime, this.formInline.endTime)
      if (this.formInline.beginTime == undefined) {
        this.formInline.beginTime = (this.regTime + '').substring(0, 10)
      } else {
        this.formInline.beginTime = (this.formInline.beginTime + '').substring(0, 10)
      }
      if (this.formInline.endTime == undefined) {
        this.formInline.endTime = (new Date().getTime() + '').substring(0, 10)
      } else {
        this.formInline.endTime = (this.formInline.endTime + '').substring(0, 10)
      }
      if (this.formInline.Type == undefined) {
        this.formInline.Type = "0"
      }
      let roles = "4";
      let Type = this.formInline.Type == '' ? "0" : this.formInline.Type
      console.log(this.formInline.beginTime, this.formInline.endTime)
      this.$get(
        `/aqdl/auditlog/getlogrecord?page=${this.pageNum}&count=${this.pageSize}&type=${Type}&role=${roles}&beginTime=${this.formInline.beginTime}&endTime=${this.formInline.endTime}&logtype=50`
      ).then((res) => {
        console.log(res);
        let datas = res.data.data.context.Root.LogRecordList.LogRecord;
        if (datas) {
          datas.forEach(item => {
            item.Type = E_actType[item.Type]
            item.KeyType = E_keyType[item.KeyType]
            item.TimeStamp = this.getLocalTime(item.TimeStamp)
          });
        } else {
          datas = []
        }
        this.tableData = datas
        this.totalCount = res.data.data.total
        this.setlogrlist("323", "0", "成功");
        // this.getloginserinfo()
      });
      this.showAuditDialog = false;
      this.formInline = {};
    },
    confirmInfo1() {
      console.log(this.formInline.beginTime, this.formInline.endTime)
      if (this.formInline.beginTime == undefined) {
        this.formInline.beginTime = (this.regTime + '').substring(0, 10)
      } else {
        this.formInline.beginTime = (this.formInline.beginTime + '').substring(0, 10)
      }
      if (this.formInline.endTime == undefined) {
        this.formInline.endTime = (new Date().getTime() + '').substring(0, 10)
      } else {
        this.formInline.endTime = (this.formInline.endTime + '').substring(0, 10)
      }
      if (this.formInline.Type == undefined) {
        this.formInline.Type = "0"
      }
      let roles = "4";
      let Type = this.formInline.Type == '' ? "0" : this.formInline.Type
      console.log(this.formInline.beginTime, this.formInline.endTime)
      this.$get(
        `/aqdl/auditlog/getlogrecord?page=${this.pageNum}&count=${this.pageSize}&type=${Type}&role=${roles}&beginTime=${this.formInline.beginTime}&endTime=${this.formInline.endTime}&logtype=50`
      ).then((res) => {
        console.log(res);
        let datas = res.data.data.context.Root.LogRecordList.LogRecord;
        if (datas) {
          datas.forEach(item => {
            item.Type = E_actType[item.Type]
            item.KeyType = E_keyType[item.KeyType]
            item.TimeStamp = this.getLocalTime(item.TimeStamp)
          });
        } else {
          datas = []
        }
        this.tableData = datas
        this.totalCount = res.data.data.total
      });
      this.showAuditDialog = false;
      this.formInline = {};
    },
    getTabpation(pageNum, pageSize) {
      console.log(pageNum, pageSize)
      this.pageNum = pageNum
      this.pageSize = pageSize
      this.confirmInfo1()
    },
    getloginserinfo() {
      this.$get('/aqdl/user/getloginuserinfo').then(res => {
        console.log(res)
        this.obsefo = res.data.data
        if (this.obsefo.bind == 1) {
          this.setlogrlist("323", "0", "成功");
        } else {

        }
      })
    },
    setTableHeight() {
      let clientHeiht =
        window.innerHeight || document.documentElement.clientHeight;
      this.tableHeight = clientHeiht - 230;
    },

    rizchaxu() {
      this.showAuditDialog = true;
    },
    auditDialogClosed() {
      // this.formInline = {
      //   Type: "",
      //   beginTime: "",
      //   endTime: "",
      // }
    },
    theImportCallback() { },
    daochus() {
      let roles = "4";
      // if (this.userName == "secadm") {
      //   roles = "4";
      // } else{
      //   roles = "0";
      // }
      let url = `/aqdl/auditlog/exportlogrecord?logtype=12&type=1&role=${roles}`;
      this.$get(url).then((res) => {
        const link = document.createElement("a");
        let blob = new Blob([res.data], { type: "application/vnd.ms-excel" }); //生成blob对象
        console.log(blob);
        link.style.display = "none";
        link.href = URL.createObjectURL(blob);
        // link.setAttribute("download", "系统备份数据" + ".vrvsys"); //下载的文件名以及文件格式
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
        console.log(res, "tree");
      });
    },
  },
};
</script>
  
<style scoped>
.disfena {
  display: flex;
  margin-top: 10px;
  margin-bottom: 8px;
}

.somius {
  margin-left: 120px;
  margin-top: 10px;
}

.zhush {
  margin-top: 30px;
  margin-left: 42%;
}

:deep(.el-date-editor.el-input, .el-date-editor.el-input__inner) {
  width: 95%;
}
</style>